package com.xiaoyu.lanling.feature.chat.handler

import android.content.DialogInterface
import android.view.View
import android.view.View.OnLongClickListener
import androidx.appcompat.app.AlertDialog
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.base.view.OnClickDebounceListener
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.event.chat.ChatMessageCallItemRecallClickEvent
import com.xiaoyu.lanling.feature.chat.model.message.ChatMessageReceiveCallItem
import com.xiaoyu.lanling.feature.chat.model.message.ChatMessageSendCallItem
import com.xiaoyu.lanling.feature.im.provider.MessageDataProvider
import com.xiaoyu.lanling.feature.im.provider.MessageDataProvider.Companion.instance

object ChatMessageCallItemClickHandler {

    private val receiveVoiceOnLongClickListener = OnLongClickListener { v: View ->
        val itemData: ChatMessageReceiveCallItem = v.getViewTag<ChatMessageReceiveCallItem>()
                ?: return@OnLongClickListener false
        val items = arrayOf<CharSequence>(AppContext.getString(R.string.action_delete))
        AlertDialog.Builder(v.context)
                .setItems(items) { dialog: DialogInterface, which: Int ->
                    when (which) {
                        0 -> instance.deleteMessageAsync(itemData.message)
                    }
                    dialog.dismiss()
                }.show()
        true
    }

    private val receiveVoiceOnClickListener: OnClickDebounceListener = object : OnClickDebounceListener() {
        override fun onClickDebounce(v: View) {
            val itemData: ChatMessageReceiveCallItem = v.getViewTag<ChatMessageReceiveCallItem>()
                    ?: return
            ChatMessageCallItemRecallClickEvent().post()
        }
    }

    private val sendVoiceOnLongClickListener = OnLongClickListener { v: View ->
        val itemData: ChatMessageSendCallItem = v.getViewTag<ChatMessageSendCallItem>()
                ?: return@OnLongClickListener false
        val items = arrayOf<CharSequence>(AppContext.getString(R.string.action_delete))
        AlertDialog.Builder(v.context)
                .setItems(items) { dialog: DialogInterface, which: Int ->
                    when (which) {
                        0 -> MessageDataProvider.instance.deleteMessageAsync(itemData.message)
                    }
                    dialog.dismiss()
                }.show()
        true
    }

    private val sendVoiceOnClickListener: OnClickDebounceListener = object : OnClickDebounceListener() {
        override fun onClickDebounce(v: View) {
            val itemData: ChatMessageSendCallItem = v.getViewTag<ChatMessageSendCallItem>()
                    ?: return
            ChatMessageCallItemRecallClickEvent().post()
        }
    }

    fun bindReceiveVoiceItemClickForViewAndData(view: View?, itemData: ChatMessageReceiveCallItem?) {
        view?.setOnClickDebounceListener(receiveVoiceOnClickListener)
        view?.setViewTag(itemData)
    }

    fun bindReceiveVoiceItemLongClickForViewAndData(view: View?, itemData: ChatMessageReceiveCallItem?) {
        view?.setOnLongClickListener(receiveVoiceOnLongClickListener)
        view?.setViewTag(itemData)
    }

    fun bindSendCallItemClickForViewAndData(view: View?, itemData: ChatMessageSendCallItem?) {
        view?.setOnClickDebounceListener(sendVoiceOnClickListener)
        view?.setViewTag(itemData)
    }

    fun bindSendCallItemLongClickForViewAndData(view: View?, itemData: ChatMessageSendCallItem?) {
        view?.setOnLongClickListener(sendVoiceOnLongClickListener)
        view?.setViewTag(itemData)
    }
}